/**
Copyright [2008] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package aerith.swing;

import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JPanel;

import aerith.components.AerithSplashScreen;

public class AerithSplash extends JPanel {

	private boolean latch = true;

	/**
	 * 
	 */
	private static final long serialVersionUID = -3960171689911108454L;

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (latch) {
			initSplashScreen();
			latch = false;
		}
	}

	private void initSplashScreen() {
		setVisible(false);
		/*if (super.getRootPane().getParent() instanceof JFrame) {
			Component c = javax.swing.SwingUtilities.getRoot(this);
			String className = c.getClass().getCanonicalName();
			if (!"org.netbeans.core.windows.view.ui.MainWindow"
					.equalsIgnoreCase(className)) {*/
		if (!java.beans.Beans.isDesignTime()) {
				JFrame parentFrame = (JFrame) super.getRootPane().getParent();
				AerithSplashScreen splash = new AerithSplashScreen();
				splash.setParentFrame(parentFrame);
				//JLayeredPane splash = new AerithSplashScreen();
				//AerithSplashScreen splash = new AerithSplashScreen();
				//splash.setParentFrame(parentFrame);
				
				parentFrame.getContentPane().setVisible(false);
				parentFrame.setGlassPane(splash);
				splash.setVisible(true);
			} else {
				// Inside netbeans demo window
			}
		//}
	}
	
	public File getImagePath() {
		return new File(AerithSplashScreen.getLogo());
	}
	
	public void setImagePath(File imagePath){
		AerithSplashScreen.setLogo(imagePath.getAbsolutePath());
		if (getPreferredSize().height==0)
			setPreferredSize(new Dimension(100, 100));
	}
	
	public Dimension getPreferredSize() {
		Dimension ps = super.getPreferredSize();
		if (ps.height==0)
			return new Dimension(100,100);
		else
			return ps;
	}

}
